package com.ruoyi.labdata.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.labdata.domain.LabReservation;
import com.ruoyi.labdata.service.ILabReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 实验室预约Controller
 *
 * @author ruoyi
 * @date 2022-03-27
 */
@RestController
@RequestMapping("/labdata/reservation")
public class LabReservationController extends BaseController {
    @Autowired
    private ILabReservationService labReservationService;

    /**
     * 查询实验室预约列表
     */
    @PreAuthorize("@ss.hasPermi('labdata:reservation:query')")
    @GetMapping("/list")
    public TableDataInfo list(LabReservation labReservation) {
        startPage();
        List<LabReservation> list = labReservationService.selectLabReservationList(labReservation);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('labdata:blacklist:query')")
    @GetMapping("/blacklist")
    public TableDataInfo blacklist(LabReservation labReservation) {
        startPage();
        List<LabReservation> list = labReservationService.queryLabReservationBlackList(labReservation);
        return getDataTable(list);
    }

    /**
     * 导出实验室预约列表
     */
   /* @PreAuthorize("@ss.hasPermi('labdata:reservation:export')")
    @Log(title = "实验室预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LabReservation labReservation) {
        List<LabReservation> list = labReservationService.selectLabReservationList(labReservation);
        ExcelUtil<LabReservation> util = new ExcelUtil<LabReservation>(LabReservation.class);
        return util.exportExcel(list, "reservation");
    }*/

    /**
     * 获取实验室预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('labdata:reservation:query')")
    @GetMapping(value = "/{instanceId}")
    public AjaxResult getInfo(@PathVariable("instanceId") Long instanceId) {
        return AjaxResult.success(labReservationService.selectLabReservationById(instanceId));
    }

    /**
     * 新增实验室预约
     */
    @PreAuthorize("@ss.hasPermi('labdata:reservation:edit')")
    @Log(title = "实验室预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LabReservation labReservation) {
        labReservationService.insertLabReservation(labReservation);
        labReservation.setImages(labReservationService.getInstanceFiles(labReservation.getInstanceId()));
        return AjaxResult.success(labReservation);
    }

    /**
     * 修改实验室预约
     */
    @PreAuthorize("@ss.hasPermi('labdata:reservation:edit')")
    @Log(title = "实验室预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LabReservation labReservation) {
        labReservationService.updateLabReservation(labReservation);
        labReservation.setImages(labReservationService.getInstanceFiles(labReservation.getInstanceId()));
        return AjaxResult.success(labReservation);
    }

    /**
     * 删除实验室预约
     */
    @PreAuthorize("@ss.hasPermi('labdata:reservation:edit')")
    @Log(title = "实验室预约", businessType = BusinessType.DELETE)
    @DeleteMapping("/{instanceIds}")
    public AjaxResult remove(@PathVariable Long[] instanceIds) {
        return toAjax(labReservationService.deleteLabReservationByIds(instanceIds));
    }
    @PreAuthorize("@ss.hasPermi('labdata:reservation:edit')")
    @Log(title = "提交实验室预约流程", businessType = BusinessType.UPDATE)
    @PostMapping("/commit")
    public AjaxResult commit(@RequestBody LabReservation labReservation) {
        return AjaxResult.success(labReservationService.commitLabReservation(labReservation));
    }
}
